//
//  HSYNewClertViewController.m
//  HSY
//
//  Created by 陈浩 on 2017/4/10.
//  Copyright © 2017年 金开门. All rights reserved.
//

#import "HSYNewClertViewController.h"
#import "HSYShopManageViewController.h"
#import "HSYNewClerkRequest.h"
#import "NSString+Validation.h"
#import "BQActivityView.h"

@interface HSYNewClertViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UIButton *shopownerRoleBtn;
@property (weak, nonatomic) IBOutlet UIButton *clerkRoleBtn;

@property (weak, nonatomic) IBOutlet UITextField *realNameTextFiled;
@property (weak, nonatomic) IBOutlet UITextField *phoneTextFiled;
@property (weak, nonatomic) IBOutlet UITextField *shopNameTextFiled;

@property(nonatomic,strong)HSYNewClerkRequest *task;

@end

@implementation HSYNewClertViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.titleButton setTitle:@"新增店员" forState:UIControlStateNormal];

    self.shopownerRoleBtn.layer.borderColor = [UIColor colorWithHexString:@"bebebe"].CGColor;
    self.shopownerRoleBtn.layer.borderWidth = 0.5;
    [self.shopownerRoleBtn setTitleColor:[UIColor colorWithHexString:@"6284dd"] forState:UIControlStateSelected];

    self.clerkRoleBtn.layer.borderColor = [UIColor colorWithHexString:@"bebebe"].CGColor;
    self.clerkRoleBtn.layer.borderWidth = 0.5;
    [self.clerkRoleBtn setTitleColor:[UIColor colorWithHexString:@"6284dd"] forState:UIControlStateSelected];
    
    
    [self settlementTypeBtn:self.clerkRoleBtn backgroundColor:[UIColor whiteColor] borderColor:[UIColor colorWithHexString:@"bebebe"]];
    
    self.shopNameTextFiled.delegate = self;
    
    
    self.task.parentID = [[HSYUserCenter instatce].appAuUser.uid integerValue];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)settlementTypeBtn:(UIButton *)btn  backgroundColor:(UIColor *)bgcolor borderColor:(UIColor *) borderColor{
    btn.clipsToBounds = YES;
    btn.layer.borderColor = borderColor.CGColor;
    btn.backgroundColor = bgcolor;
    btn.layer.borderWidth = 0.5;
}

- (IBAction)shopownerRoleBtnClcik:(id)sender {
    self.task.role = 2; //店长
    
    self.shopownerRoleBtn.selected = YES;
    self.clerkRoleBtn.selected = NO;
    
    [self settlementTypeBtn:self.shopownerRoleBtn backgroundColor:[UIColor colorWithHexString:@"f2f6ff"] borderColor:[UIColor colorWithHexString:@"7a97e3"]];
    [self settlementTypeBtn:self.clerkRoleBtn backgroundColor:[UIColor whiteColor] borderColor:[UIColor colorWithHexString:@"bebebe"]];
}

- (IBAction)clerkRoleBtnClick:(id)sender {
    self.task.role = 3; //店员
    
    self.shopownerRoleBtn.selected = NO;
    self.clerkRoleBtn.selected = YES;
    
    [self settlementTypeBtn:self.clerkRoleBtn backgroundColor:[UIColor colorWithHexString:@"f2f6ff"] borderColor:[UIColor colorWithHexString:@"7a97e3"]];
    [self settlementTypeBtn:self.shopownerRoleBtn backgroundColor:[UIColor whiteColor] borderColor:[UIColor colorWithHexString:@"bebebe"]];
    
}


- (IBAction)comitBtnClick:(id)sender {
    self.task.realname = self.realNameTextFiled.text;
    self.task.cellphone = self.phoneTextFiled.text;

    if (!self.task.realname.length) {
        [self showMessage:@"请填写店员姓名"];
        return ;
    }
    
    if (!self.task.cellphone.length ) {
        [self showMessage:@"请填写店员手机号"];
        return ;
    }
    
    if (![self.task.cellphone isValidPhone] ) {
        [self showMessage:@"请填写正确的手机号"];
        return ;
    }
    
    if (!self.task.role) {
        [self showMessage:@"请选择职位类型"];
        return ;
    }
    
    if (!self.task.sid) {
        [self showMessage:@"请选择店铺"];
        return ;
    }
    
    [BQActivityView showActiviTy];
    __weak typeof(self)weakSelf = self;
    
    [HSYNetworkManager POST:self.task success:^(HSYResponse *response) {
        [BQActivityView hideActiviTy];
        
        [weakSelf showMessage:response.resultMessage];
        if (response.resultCode == HSY_REQUEST_CODE_SUCCESS) {
            //延迟0.5秒返回
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                if (weakSelf.hsyNewClerkSuccessBlock) {
                    weakSelf.hsyNewClerkSuccessBlock();
                }
                
                [weakSelf.navigationController popViewControllerAnimated:YES];

            });

        }
    } failure:^(NSError *error) {
        [BQActivityView hideActiviTy];
        
        if (error.code != NSURLErrorCancelled) {
            [weakSelf showMessage:@"请求超时"];
        }
    }];
}


- (IBAction)shopNameTextFileTouchDown:(id)sender {
    
    HSYLog(@"shopNameTextFileTouchDown");
    
    [self.view endEditing:YES];
    
    HSYShopManageViewController *vc = [[HSYShopManageViewController alloc] init];
    vc.isSelectShop = YES;
    __weak typeof(self)weakSelf = self;
    vc.selectShopBlock = ^(HSYShopInfoModel *model, HSYShopManageViewController *viewController){
        weakSelf.task.sid = [model.shopID integerValue];
        weakSelf.shopNameTextFiled.text = model.shortName.length > 0 ? model.shortName : model.name;
    };
    
    [self.navigationController pushViewController:vc animated:YES];
    
}


- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    return NO;
}


-(HSYNewClerkRequest *)task{
    if (!_task) {
        _task = [[HSYNewClerkRequest alloc] init];
    }
    return _task;
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
